package com.hltx.controller.app.business;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.hltx.controller.base.BaseController;
import com.hltx.pojo.business.BusinessInfo;
import com.hltx.service.business.BusinessManager;
import com.hltx.service.picture.PictureManager;
import com.hltx.service.user.UserManager;
import com.hltx.util.ResultInfo;

@Controller
@RequestMapping(value = "/app/business/")
public class AppBusinessController extends BaseController {
     
	@Resource(name = "userService")
	private UserManager userService;
	
	@Resource(name = "businessService")
	private BusinessManager businessService;

	@Resource(name = "PictureService")
	private PictureManager PictureService;
	
	
	
	
	//商家分类列表
	@RequestMapping(value = "bClassList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> bClassList(HttpServletRequest request,HttpServletResponse response) {
		this.logBefore(logger, "查询商家分类列表");
		Map<String, Object> resMap=new HashMap<>();
		try{
			List<Map<String, Object>> bList=businessService.bClassList();
			resMap.put("bList", bList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		this.logAfter(logger);		
		return resMap;
	}
	  //查询商家状态
	@RequestMapping(value = "getState", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getState(HttpServletResponse response, HttpServletRequest request) {
			this.logBefore(logger, "查询商家审核状态");
			String userId=request.getParameter("userId");
			Map<String, Object> params=new HashMap<>();
			params.put("userId",userId );
			Map<String, Object> resMap = new HashMap<String, Object>();
			try {
			if("".equals(userId)||null==userId){
			resMap.put("result", this.initResult(ResultInfo.ERROR, "userError", "用户未登录"));
			return resMap;
			}
			String state=businessService.getState(params);
			resMap.put("state", state);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
			}catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
			}
			return resMap;
			
	}	
		
	//保存商家信息
	@RequestMapping(value = "saveBusiness", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> saveBusiness(BusinessInfo businessInfo,HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "保存商家信息");
		Map<String, Object> resMap = new HashMap<String, Object>();	
		try {	
		     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			 //file 是指 文件上传标签的 name=值 ,根据 name 获取上传的文件...  
			 List<MultipartFile> fileList = multipartRequest.getFiles("aPhotos");
			 MultipartFile businessPhot= multipartRequest.getFile("businessPhotos"); 
			 MultipartFile cardPhot = multipartRequest.getFile("cardPhotos"); 
			 if(fileList.size()<=0||null==fileList||null==businessPhot||"".equals(businessPhot)||null==cardPhot||"".equals(cardPhot)){
				  resMap.put("result", this.initResult(ResultInfo.ERROR, "uplost", "上传文件不能为空!"));
				  return resMap;
			 }
			 Map<String, Object> multMap=new HashMap<>();
			 multMap.put("fileList", fileList);
			 multMap.put("businessPhot", businessPhot);
			 multMap.put("cardPhot", cardPhot);
			 String message=businessService.saveBusiness(businessInfo,multMap);
		    if("countError".equals(message)){
			resMap.put("result", this.initResult(ResultInfo.ERROR, "countFailed", "商家已存在!"));	
			}else if("discountError".equals(message)){
			resMap.put("result", this.initResult(ResultInfo.ERROR, "discountError", "折扣不存在"));
			}else{
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "保存成功"));
			}
		} catch (IOException e1) {
			e1.printStackTrace();
			this.logError(logger, e1.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "upfailed", "文件上传失败!"));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		return resMap;
	}
}
